package gold.digger;

import gold.utils.InputUtil;

import java.util.TreeSet;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC220 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
            TreeSet<Long> set = new TreeSet<>();
            int left = 0, right = 0;

            while (right < nums.length) {
                if (right - left > k) {
                    // window too large
                    set.remove(((long) nums[left++]));
                }

                // Find the successor of current element
                Long s = set.ceiling(((long) nums[right]));
                if (s != null && s <= nums[right] + t) return true;

                // Find the predecessor of current element
                Long g = set.floor(((long) nums[right]));
                if (g != null && nums[right] <= g + t) return true;

                set.add(((long) nums[right++]));
            }

            return false;
        }
    }


    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[2147483640,2147483641]");
        System.out.println(solution.containsNearbyAlmostDuplicate(arr, 1, 100));
    }

    public static void main(String[] args) throws Exception {
        LC220 an = new LC220();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
